package cn.lhjl.leetcode;

public class 二分查找 {
    public static void main(String[] args) {
        int[] a1={1,2,3,4,5,6,7};
        System.out.println(binarySearch(a1, 7));
    }
    public static int binarySearch(int[] arr,int target){
        int start = 0;
        int end = arr.length-1;
        int middle;
        while (start<=end){
            middle=(start+end)/2;
            if (arr[middle] == target) {
                return middle;
            }else if (arr[middle]>target){
                end=middle-1;
            }else{
                start=middle+1;
            }
        }
        return -1;
    }

}
